using System;
using System.Data;
using System.Windows.Forms;
using TUnit.Data;
using TUnit.Class;

namespace viclip
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CVideos : Class
    {
        #region Declares
        int _videoid = -1;
        string _videoTitle = "";
        int _videoIndex = -1;
        string _linkvideo = "";
        string _logo = "";
        string _description = "";
        int _userviews = -1;
        DateTime _timeupdate = DateTime.Now;
        bool _videostatus = false;
        int _catalogid = -1;
        string _price = "FREE";

        public string Price
        {
            get { return _price; }
            set { _price = value; }
        }
        #endregion

        #region Constructors
        public CVideos()
        {
        }
        public CVideos(CLogin Login)
        {
            base.Login = Login;
        }
        public CVideos(CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBVideos db = new AccDBVideos())
            {
                this.Populate((CVideos)db.GetClass(Login, ObjId));
            }
        }
        public CVideos(CLogin Login, IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CVideos(CLogin Login, IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBVideos db = new AccDBVideos())
            {
                this.Populate((CVideos)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("videoid") >= 0)
                    {
                        this._videoid = (dr["videoid"] != DBNull.Value ? Convert.ToInt32(dr["videoid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblVideos ADD videoid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("videoTitle") >= 0)
                    {
                        this._videoTitle = (dr["videoTitle"] != DBNull.Value ? Convert.ToString(dr["videoTitle"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblVideos ADD videoTitle [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("videoIndex") >= 0)
                    {
                        this._videoIndex = (dr["videoIndex"] != DBNull.Value ? Convert.ToInt32(dr["videoIndex"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblVideos ADD videoIndex [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("linkvideo") >= 0)
                    {
                        this._linkvideo = (dr["linkvideo"] != DBNull.Value ? Convert.ToString(dr["linkvideo"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblVideos ADD linkvideo [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("logo") >= 0)
                    {
                        this._logo = (dr["logo"] != DBNull.Value ? Convert.ToString(dr["logo"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblVideos ADD logo [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("description") >= 0)
                    {
                        this._description = (dr["description"] != DBNull.Value ? Convert.ToString(dr["description"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblVideos ADD description [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("userviews") >= 0)
                    {
                        this._userviews = (dr["userviews"] != DBNull.Value ? Convert.ToInt32(dr["userviews"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblVideos ADD userviews [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("timeupdate") >= 0)
                    {
                        this._timeupdate = (dr["timeupdate"] != DBNull.Value ? Convert.ToDateTime(dr["timeupdate"]) : DateTime.Now);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblVideos ADD timeupdate [datetime] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("videostatus") >= 0)
                    {
                        this._videostatus = (dr["videostatus"] != DBNull.Value ? Convert.ToBoolean(dr["videostatus"]) : false);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblVideos ADD videostatus [bit] NULL ");
                    //}
                    if (dr.Table.Columns.IndexOf("catalogid") >= 0)
                    {
                        this._catalogid = (dr["catalogid"] != DBNull.Value ? Convert.ToInt32(dr["catalogid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblVideos ADD catalogid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("price") >= 0)
                    {
                        this._price = (dr["price"] != DBNull.Value ? Convert.ToString(dr["price"]) : "FREE");
                    }
                }
                catch { }
            }
        }
        public void Populate(CVideos Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._videoid = Obj.videoid;
                    this._videoTitle = Obj.videoTitle;
                    this._videoIndex = Obj.videoIndex;
                    this._linkvideo = Obj.linkvideo;
                    this._logo = Obj.logo;
                    this._description = Obj.description;
                    this._userviews = Obj.userviews;
                    this._timeupdate = Obj.timeupdate;
                    this._videostatus = Obj.videostatus;
                    this._catalogid = Obj.catalogid;
                    this._price = Obj.Price;
                }
                catch { }
            }
        }
        public override bool Save()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBVideos db = new AccDBVideos())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(IProvider p)
        {
            using (AccDBVideos db = new AccDBVideos())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBVideos db = new AccDBVideos())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(IProvider p)
        {
            using (AccDBVideos db = new AccDBVideos())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override Form PrintPreview()
        {
            throw (new NotSupportedException("Phuong thuc nay hien chua duoc cai dat!"));
        }
        public override bool Print()
        {
            throw (new NotSupportedException("Phuong thuc nay hien chua duoc cai dat!"));
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CVideos obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CVideos Videos);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CVideos Videos);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CVideos Videos);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CVideos Videos);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CVideos obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int videoid
        {
            get { return this._videoid; }
            set
            {
                if (this._videoid != value)
                {
                    this._videoid = value;
                }
            }
        }
        public string videoTitle
        {
            get { return this._videoTitle; }
            set
            {
                if (this._videoTitle != value)
                {
                    this._videoTitle = value;
                }
            }
        }
        public int videoIndex
        {
            get { return this._videoIndex; }
            set
            {
                if (this._videoIndex != value)
                {
                    this._videoIndex = value;
                }
            }
        }
        public string linkvideo
        {
            get { return this._linkvideo; }
            set
            {
                if (this._linkvideo != value)
                {
                    this._linkvideo = value;
                }
            }
        }
        public string logo
        {
            get { return this._logo; }
            set
            {
                if (this._logo != value)
                {
                    this._logo = value;
                }
            }
        }
        public string description
        {
            get { return this._description; }
            set
            {
                if (this._description != value)
                {
                    this._description = value;
                }
            }
        }
        public int userviews
        {
            get { return this._userviews; }
            set
            {
                if (this._userviews != value)
                {
                    this._userviews = value;
                }
            }
        }
        public DateTime timeupdate
        {
            get { return this._timeupdate; }
            set
            {
                if (this._timeupdate != value)
                {
                    this._timeupdate = value;
                }
            }
        }
        public bool videostatus
        {
            get { return this._videostatus; }
            set
            {
                if (this._videostatus != value)
                {
                    this._videostatus = value;
                }
            }
        }
        public int catalogid
        {
            get { return this._catalogid; }
            set
            {
                if (this._catalogid != value)
                {
                    this._catalogid = value;
                }
            }
        }
        #endregion
    }

    #region Class AccDB
    public class AccDBVideos : AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBVideos()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(CLogin Login, ComboBox cmb, string extName)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT videoid, VideosName";
                sql += " FROM  tblVideos";
                sql += " WHERE (videoid >= 0)";
                sql += " ORDER BY VideosName";
                DataTable table = p.OpenTable(sql, "tblVideos");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["VideosName"] = extName;
                        row["videoid"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "VideosName";
                    cmb.ValueMember = "videoid";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override Class GetClass(CLogin Login, IProvider p, object ObjId)
        {
            try
            {
                string sql = "SELECT * FROM tblVideos WHERE (videoid = " + ObjId + ")";
                DataTable table = p.OpenTable(sql, "tblVideos");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return new CVideos(Login, p, table.Rows[0]);
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(IProvider p, Class obj)
        {
            CVideos Videos = (CVideos)obj;
            string sql = "SELECT * FROM tblVideos";
            sql += " WHERE videoid =" + Videos.videoid + "";
            //sql +=" AND CompanyId =" + Videos.CompanyId + "";
            DataTable dt = p.OpenTable(sql, "tblVideos");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1 && Videos.videoid != -1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    Videos.videoid = Convert.ToInt32(p.InitIDKey("videoid", "tblVideos", null));
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["videoid"] = Videos.videoid;
                row["videoTitle"] = Videos.videoTitle;
                row["videoIndex"] = Videos.videoIndex;
                row["linkvideo"] = Videos.linkvideo;
                row["logo"] = Videos.logo;
                row["description"] = Videos.description;
                row["userviews"] = Videos.userviews;
                row["timeupdate"] = Videos.timeupdate;
                row["videostatus"] = Videos.videostatus;
                row["catalogid"] = Videos.catalogid;
                row["price"] = Videos.Price;
                if (!p.UpdateTable("tblVideos", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(IProvider p, Class obj)
        {
            CVideos Videos = (CVideos)obj;
            string sql = "DELETE FROM tblVideos";
            sql += " WHERE videoid =" + Videos.videoid + "";
            //sql +=" AND CompanyId =" + Videos.CompanyId + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(CLogin Login, IProvider p, CCollection Collection)
        {
            string sql = "SELECT * FROM tblVideos ORDER BY videoid";
            DataTable table = p.OpenTable(sql, "tblVideos");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CVideos o = new CVideos(Login, p, row);
                    Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public void GetCollectionCD101(CLogin Login, CCollection Collection)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT * FROM tblVideos WHERE catalogid = 0 ORDER BY videoid";
                DataTable table = p.OpenTable(sql, "tblVideos");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CVideos o = new CVideos(Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public void GetCollectionTomAndJerry(CLogin Login, CCollection Collection)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT * FROM tblVideos WHERE catalogid = 6 ORDER BY videoid";
                DataTable table = p.OpenTable(sql, "tblVideos");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CVideos o = new CVideos(Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public void GetCollectionCHILDREN(CLogin Login, CCollection Collection)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT * FROM tblVideos WHERE catalogid = 10 ORDER BY videoid";
                DataTable table = p.OpenTable(sql, "tblVideos");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CVideos o = new CVideos(Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public void GetCollectionByCat(CLogin Login, CCollection Collection, string catalogid)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT * FROM tblVideos WHERE catalogid = " + catalogid + " ORDER BY videoIndex, userviews DESC";
                DataTable table = p.OpenTable(sql, "tblVideos");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CVideos o = new CVideos(Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public void GetCollectionTopNew(CLogin Login, CCollection Collection)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT TOP 25 * FROM tblVideos WHERE (catalogid=8)OR(catalogid=13)OR(catalogid=14)OR(catalogid=15)OR(catalogid=1) ORDER BY timeupdate DESC";
                DataTable table = p.OpenTable(sql, "tblVideos");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CVideos o = new CVideos(Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
        // <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public void GetCollectionTopHot(CLogin Login, CCollection Collection)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT TOP 25 * FROM tblVideos WHERE (catalogid=8)OR(catalogid=13)OR(catalogid=14)OR(catalogid=15)OR(catalogid=1) ORDER BY userviews DESC";
                DataTable table = p.OpenTable(sql, "tblVideos");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CVideos o = new CVideos(Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(IProvider p, Class obj, CCollection Collection)
        {
        }
    }
    #endregion
}
